package com.dituhui.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.dituhui.tool.io.Constants;
import com.dituhui.tool.io.ESTool;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 查看上传图片
 * 
 * @author liweigu
 * 
 */
public class GetImage extends HttpServlet {
	private static Logger LOGGER = Logger.getLogger(GetImage.class);
	private String historyESUrl;

	public void init() throws ServletException {
		this.historyESUrl = Constants.getValue("HistoryESUrl", "gps_config.properties");
	}

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String fileName = request.getParameter("name");
		String userId = request.getParameter("user");
		// day的格式为 YYYY-MM-DD
		String picDay = request.getParameter("day");
		// 跨域访问
		String callbacks = request.getParameter("callbacks");
		if (fileName != null && fileName.length() > 0) {
			// 查看图片
			// String filePath = DoUploadFile.uploadPicPath;
			// if (DoUploadFile.VIDEO.equalsIgnoreCase(contentType)) {
			// filePath = DoUploadFile.uploadVideoPath;
			// }
			// String picPath = filePath + StringUtil.SLASH + fileName;
			// File file = new File(picPath);
			// BufferedImage bufferedImage = ImageIO.read(file);
			// ServletOutputStream servletOutputStream = response.getOutputStream();
			// String postfix = fileName.substring(fileName.indexOf(".") + 1);
			// String formatName = postfix;
			// ImageIO.write(bufferedImage, formatName, servletOutputStream);
			// servletOutputStream.flush();
		} else if (userId != null && userId.length() > 0 && picDay != null && picDay.length() > 0) {
			String[] queryColumnNames = { "userId", "time" };
			String[] queryColumnValues = { userId, picDay };
			List<String> pics = ESTool.query(DoUploadFile.uploadPicIndex, queryColumnNames, queryColumnValues, this.historyESUrl);
			if (pics != null) {
				LOGGER.debug("pics.size()=" + pics.size());
			}
			List<String> videos = ESTool.query(DoUploadFile.uploadVideoIndex, queryColumnNames, queryColumnValues, this.historyESUrl);
			if (videos != null) {
				LOGGER.debug("videos.size()=" + videos.size());
			}
			List<String> results = new ArrayList<String>();
			for (String pic : pics) {
				JSONObject jsonObject = JSONObject.fromObject(pic);
				jsonObject.accumulate("contentType", DoUploadFile.PIC);
				// String id = jsonObject.getString("userId");
				// String time = jsonObject.getString("time");
				// String imgNames = jsonObject.getString("imgNames");
				// String coord = jsonObject.getString("coord");
				results.add(jsonObject.toString());
			}
			for (String video : videos) {
				JSONObject jsonObject = JSONObject.fromObject(video);
				jsonObject.accumulate("contentType", DoUploadFile.VIDEO);
				results.add(jsonObject.toString());
			}

			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			JSONArray jsonArray = JSONArray.fromObject(results);
			String output = jsonArray.toString();
			if (callbacks != null && callbacks.length() > 0) {
				// 包装callbacks，支持跨越访问
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.append(callbacks);
				stringBuilder.append("(");
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("data", output);
				stringBuilder.append(jsonObject.toString());
				stringBuilder.append(")");
				output = stringBuilder.toString();
			}
			out.print(output);
			out.flush();
			out.close();
		}
	}
}
